/**
  ******************************************************************************
  * File Name          : envirctrl.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "tskcfg.h"
#include "network.h"
#include "route.h"
#include "cJSON.h"
#include "dwlcd.h"

#if (ROUTE_XC_SUBDEV_VERSION != 1001)
  #error "xc route version error!"
#endif

typedef struct
{
	cJSON *root;
  cJSON *params;
  cJSON *key;
  char  split_buf[64];
}route_xc_down_static_type;
/* 本文件中调用的一些变量,为了减少栈的消耗,定义到函数外的全局变量,使用前手动清0 */
static route_xc_down_static_type xc_down_static;

/* nrxp:{"id":"9","version":"1.0","params":{"zsb":"XC60210310011"}} */
uint8_t Route_xc_down_fw(const char *str, uint8_t len)
{
	uint8_t err = 0;
	uint8_t dev_index = 0;
  Route_PayloadType *proute = NULL;
  
  proute = pvPortMalloc(sizeof(Route_PayloadType));
  if(proute == NULL)
  {
    return 1;
  }
  
  memset(proute, 0, sizeof(Route_PayloadType));
  memset(&xc_down_static, 0, sizeof(route_xc_down_static_type));
  
/* *** */ 	
  xc_down_static.root = cJSON_Parse(str);
  
  xc_down_static.params = cJSON_GetObjectItem(xc_down_static.root, "params");
	if(xc_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }
	

/* *** */  
	xc_down_static.key = cJSON_GetObjectItem(xc_down_static.params, "zsb");
	if((xc_down_static.key == NULL) || (xc_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(xc_down_static.key->valuestring, &dev_index) != 0)
	{
		err = 3;
    goto fnout;
	}
	
	proute->dev_addr  = idx2add(dev_index);
	proute->dev_type  = RT_DV_XC;
	proute->pl_type   = ROUTE_PLTYP_XC_RESET;
	proute->trans_dir = ROUTE_TRSDIR_NET_TO_SUBDEV;
  proute->pl_len    = 0;
  
	if(Route_EvewTo485_Qhd != NULL)
	{
		xQueueSend(Route_EvewTo485_Qhd, proute, 0);
	}
	
fnout:
  cJSON_Delete(xc_down_static.key);
  cJSON_Delete(xc_down_static.params);
  cJSON_Delete(xc_down_static.root);
	
  vPortFree(proute);
  
  return err;
}

/* nrxp:{"id":"6","version":"1.0","params":{"xc_khd":"89","zsb":"XC60210310011","xc_cs":"23"}} */
uint8_t Route_xc_down_sj(const char *str, uint8_t len)
{
	uint8_t err = 0;
	uint8_t dev_index = 0;
  int value;
  Route_PayloadType *proute = NULL;
  
  proute = pvPortMalloc(sizeof(Route_PayloadType));
  if(proute == NULL)
  {
    return 1;
  }
  
  memset(proute, 0, sizeof(Route_PayloadType));
  memset(&xc_down_static, 0, sizeof(route_xc_down_static_type));
  
/* *** */ 	
  xc_down_static.root = cJSON_Parse(str);
  
  xc_down_static.params = cJSON_GetObjectItem(xc_down_static.root, "params");
	if(xc_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }
	

/* *** */  
	xc_down_static.key = cJSON_GetObjectItem(xc_down_static.params, "zsb");
	if((xc_down_static.key == NULL) || (xc_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(xc_down_static.key->valuestring, &dev_index) != 0)
	{
		err = 3;
    goto fnout;
	}
	
  
/************/
	xc_down_static.key = cJSON_GetObjectItem(xc_down_static.params, "xc_khd");
	if((xc_down_static.key == NULL) || (xc_down_static.key->type != cJSON_String))
  {
    err = 4;
    goto fnout;
  }
  
  value = atoi(xc_down_static.key->valuestring);
  if((value >= 0) && (value <= 101))
  {
    ((Xc_SetParaType *)(proute->payload))->curt_pos = value;
  }
  else
	{
		err = 4;
		goto fnout;
	}
	
/************/
  xc_down_static.key = cJSON_GetObjectItem(xc_down_static.params, "xc_cs");
	if((xc_down_static.key == NULL) || (xc_down_static.key->type != cJSON_String))
  {
    err = 5;
    goto fnout;
  }
  
  value = atoi(xc_down_static.key->valuestring);
  if((value >= 90) && (value <= 250))
  {
    ((Xc_SetParaType *)(proute->payload))->out_time = value;
  }
  else
	{
		err = 6;
		goto fnout;
	}
  
  
	proute->dev_addr  = idx2add(dev_index);
	proute->dev_type  = RT_DV_XC;
	proute->pl_type   = ROUTE_PLTYP_XC_SETPARA;
	proute->trans_dir = ROUTE_TRSDIR_NET_TO_SUBDEV;
  proute->pl_len    = sizeof(Xc_SetParaType);
  
	if(Route_EvewTo485_Qhd != NULL)
	{
		xQueueSend(Route_EvewTo485_Qhd, proute, 0);
	}
	
fnout:
  cJSON_Delete(xc_down_static.key);
  cJSON_Delete(xc_down_static.params);
  cJSON_Delete(xc_down_static.root);
	
  vPortFree(proute);
  
  return err;
}

uint8_t Route_xc_down_tb(const char *str, uint8_t len)
{
	uint8_t err = 0;
	uint8_t dev_index = 0;
  Route_PayloadType *proute = NULL;
  
  proute = pvPortMalloc(sizeof(Route_PayloadType));
  if(proute == NULL)
  {
    return 1;
  }
  
  memset(proute, 0, sizeof(Route_PayloadType));
  memset(&xc_down_static, 0, sizeof(route_xc_down_static_type));
  
/* *** */ 	
  xc_down_static.root = cJSON_Parse(str);
  
  xc_down_static.params = cJSON_GetObjectItem(xc_down_static.root, "params");
	if(xc_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }
	

/* *** */  
	xc_down_static.key = cJSON_GetObjectItem(xc_down_static.params, "zsb");
	if((xc_down_static.key == NULL) || (xc_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(xc_down_static.key->valuestring, &dev_index) != 0)
	{
		err = 3;
    goto fnout;
	}
	
	proute->dev_addr  = idx2add(dev_index);
	proute->dev_type  = RT_DV_XC;
	proute->pl_type   = ROUTE_PLTYP_XC_SYNC;
	proute->trans_dir = ROUTE_TRSDIR_NET_TO_SUBDEV;
  proute->pl_len    = 0;
  
	if(Route_EvewTo485_Qhd != NULL)
	{
		xQueueSend(Route_EvewTo485_Qhd, proute, 0);
	}
	
fnout:
  cJSON_Delete(xc_down_static.key);
  cJSON_Delete(xc_down_static.params);
  cJSON_Delete(xc_down_static.root);
	
  vPortFree(proute);
  
  return err;
}
